package com.johnzer.frame.utils;

import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.CalendarContract;
import android.support.annotation.Nullable;
import android.text.TextUtils;


import com.johnzer.frame.app.BaseApp;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * 日期 工具类
 */
@SuppressLint("SimpleDateFormat")
public class DateUtils {
    /**
     * @param time 需要判断的时间
     * @return 0代表上午，1代表下午
     */
    public static int morningOrAfter(long time) {
        int apm = 0;
        try {
            final Calendar mCalendar = DateUtils.getCalendar();
            mCalendar.setTimeInMillis(time);

            apm = mCalendar.get(Calendar.AM_PM);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return apm;
    }

    /**
     * 输入对应格式的时间字符串，返回相应的时间戳，parseStr 和 parseFormat格式要对应，例如yyyy-MM-dd 和 2010-06-25
     *
     * @param parseStr
     * @param parseFormat
     * @return
     * @throws ParseException
     */
    public static long getTimeByFormat(String parseStr, String parseFormat) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(parseFormat,Locale.CHINA);
        Date date = simpleDateFormat.parse(parseStr);
        long timeStemp =date.getTime();
        return timeStemp;
    }

    /**
     * 获取现在时间
     *
     * @return 返回时间类型 yyyy-MM-dd HH:mm:ss
     */
    public static Date getNowDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }

    /**
     * 获取现在时间
     *
     * @return返回短时间格式 yyyy-MM-dd
     */
    public static Date getNowDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        ParsePosition pos = new ParsePosition(8);
        Date currentTime_2 = formatter.parse(dateString, pos);
        return currentTime_2;
    }

    /**
     * 获取现在时间
     *
     * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
     */
    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间
     *
     * @return返回字符串格式 MM-dd
     */
    public static String getStringDateMd() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = getSimpleDateFormat("MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取现在时间
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取图片保存到本地时的文件的时间格式
     *
     * @return 返回短时间字符串格式yyyyMMdd_HHmmss
     */
    public static String getStringFileDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = getSimpleDateFormat("yyyyMMdd_HHmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 获取时间 小时:分;秒 HH:mm:ss
     *
     * @return
     */
    public static String getTimeShort() {
        SimpleDateFormat formatter = getSimpleDateFormat("HH:mm:ss");
        Date currentTime = new Date();
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
     *
     * @param strDate
     * @return
     */
    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 服务器上获取 短时间字符串格式 转换时间格式
     *
     * @return 返回短时间字符串格式yyyy-MM-dd
     */
    public static String getStringDateShort(String strDate) {
        try {
            SimpleDateFormat format = getSimpleDateFormat("yyyy-MM-dd");
            Long valueOf = Long.valueOf(strDate);
            String dateString = format.format(valueOf);
            return dateString;
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 获取指定数据格式时间字符串
     *
     * @param strDate   需要被转换的时间的字符串，例如“1411112311”
     * @param formatStr 需要转化成怎样的时间格式来显示，例如yyyy-MM-dd HH:mm:ss
     * @return 返回指定格式的时间字符串
     */
    public static String getStringDateByFormat(String strDate, String formatStr) {
        SimpleDateFormat format = getSimpleDateFormat(StringUtils.isEmpty(formatStr) ? "yyyy-MM-dd HH:mm" : formatStr);
        String dateString = "";
        try {
            Long valueOf = Long.valueOf(strDate);
            dateString = format.format(valueOf);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return dateString;
    }

    /**
     * 服务器上获取 短时间字符串格式 转换时间格式
     *
     * @return 返回短时间字符串格式MM-dd
     */
    public static String getStringDateMonth(String strDate) {
        SimpleDateFormat format = getSimpleDateFormat("MM-dd");
        Long valueOf = Long.valueOf(strDate);
        String dateString = format.format(valueOf);
        return dateString;
    }

    /**
     * 服务器上获取 短时间字符串格式 转换时间格式
     *
     * @return 返回短时间字符串格式HH:mm:ss
     */
    public static String getStringDateHours(String strDate) {
        SimpleDateFormat format = getSimpleDateFormat("HH:mm:ss");
        Long valueOf = Long.valueOf(strDate);
        String dateString = format.format(valueOf);
        return dateString;
    }

    /**
     * 服务器上获取 短时间字符串格式 转换时间格式
     *
     * @return 返回短时间字符串格式HH:mm:ss
     */
    public static String getStringToLong(String strDate) {
        SimpleDateFormat format = getSimpleDateFormat("yyyy-MM-dd   HH:mm:ss");
        Long valueOf = Long.valueOf(strDate);
        String dateString = format.format(valueOf);
        return dateString;
    }

    /**
     * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss * * @param dateDate * @return
     */
    public static String dateToStrLong(Date dateDate) {
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     *
     * @param dateDate
     * @return
     */
    public static String dateToStr(Date dateDate) {
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 将短时间格式字符串转换为时间 yyyy-MM-dd
     *
     * @param strDate
     * @return
     */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 得到现在时间
     *
     * @return
     */
    public static Date getNow() {
        Date currentTime = new Date();
        return currentTime;
    }


    public static void setToMidnight(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    public static Date setToMidnight(Date date) {
        Calendar calendar = DateUtils.getCalendar();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
    public static Date setToEastEightMidnight(Date date) {
        Calendar calendar = DateUtils.getCalendar();
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 提取一个月中的最后一天
     *
     * @param day
     * @return
     */
    public static Date getLastDate(long day) {
        Date date = new Date();
        long date_3_hm = date.getTime() - 3600000 * 34 * day;
        Date date_3_hm_date = new Date(date_3_hm);
        return date_3_hm_date;
    }

    /**
     * 得到现在时间
     *
     * @return 字符串 yyyy-MM-dd HH-mm-ss
     */
    public static String getStringToday() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 得到现在小时
     */
    public static String getHour() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String hour;
        hour = dateString.substring(11, 13);
        return hour;
    }

    /**
     * 得到现在分钟
     *
     * @return
     */
    public static String getTime() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        String min;
        min = dateString.substring(14, 16);
        return min;
    }

    /**
     * 根据用户传入的时间表示格式，返回当前时间的格式 如果是yyyyMMdd，注意字母y不能大写。
     *
     * @param sformat yyyyMMddhhmmss
     * @return
     */
    public static String getUserDate(String sformat) {
        Date currentTime = new Date();
        SimpleDateFormat formatter = getSimpleDateFormat(sformat);
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * 二个小时时间间的差值,必须保证二个时间都是"HH:MM"的格式，返回字符型的分钟
     */
    public static String getTwoHour(String st1, String st2) {
        String[] kk = null;
        String[] jj = null;
        kk = st1.split(":");
        jj = st2.split(":");
        if (Integer.parseInt(kk[0]) < Integer.parseInt(jj[0]))
            return "0";
        else {
            double y = Double.parseDouble(kk[0]) + Double.parseDouble(kk[1])
                    / 60;
            double u = Double.parseDouble(jj[0]) + Double.parseDouble(jj[1])
                    / 60;
            if ((y - u) > 0)
                return y - u + "";
            else
                return "0";
        }
    }

    /**
     * 得到二个日期间的间隔天数
     */
    public static String getTwoDay(String sj1, String sj2) {
        SimpleDateFormat myFormatter = getSimpleDateFormat("yyyy-MM-dd");
        long day = 0;
        try {
            Date date = myFormatter.parse(sj1);
            Date mydate = myFormatter.parse(sj2);
            day = (date.getTime() - mydate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (Exception e) {
            return "";
        }
        return day + "";
    }

    /**
     * 时间前推或后推分钟,其中JJ表示分钟.
     */
    public static String getPreTime(String sj1, String jj) {
        SimpleDateFormat format = getSimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String mydate1 = "";
        try {
            Date date1 = format.parse(sj1);
            long Time = (date1.getTime() / 1000) + Integer.parseInt(jj) * 60;
            date1.setTime(Time * 1000);
            mydate1 = format.format(date1);
        } catch (Exception e) {
        }
        return mydate1;
    }

    /**
     * 得到一个时间延后或前移几天的时间,nowdate为时间,delay为前移或后延的天数
     */
    public static String getNextDay(String nowdate, String delay) {
        try {
            SimpleDateFormat format = getSimpleDateFormat("yyyy-MM-dd");
            String mdate = "";
            Date d = strToDate(nowdate);
            long myTime = (d.getTime() / 1000) + Integer.parseInt(delay) * 24
                    * 60 * 60;
            d.setTime(myTime * 1000);
            mdate = format.format(d);
            return mdate;
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 判断是否润年
     *
     * @param ddate
     * @return
     */
    public static boolean isLeapYear(String ddate) {

        /**
         * 详细设计： 1.被400整除是闰年，否则： 2.不能被4整除则不是闰年 3.能被4整除同时不能被100整除则是闰年
         * 3.能被4整除同时能被100整除则不是闰年
         */
        Date d = strToDate(ddate);
        GregorianCalendar gc = (GregorianCalendar) DateUtils.getCalendar();
        gc.setTime(d);
        int year = gc.get(Calendar.YEAR);
        if ((year % 400) == 0)
            return true;
        else if ((year % 4) == 0) {
            if ((year % 100) == 0)
                return false;
            else
                return true;
        } else
            return false;
    }

    /**
     * 返回美国时间格式 26 Apr 2006
     *
     * @param str
     * @return
     */
    public static String getEDate(String str) {
        SimpleDateFormat formatter = getSimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(str, pos);
        String j = strtodate.toString();
        String[] k = j.split(" ");
        return k[2] + k[1].toUpperCase() + k[5].substring(2, 4);
    }

    /**
     * 获取一个月的最后一天
     *
     * @param dat
     * @return
     */
    public static String getEndDateOfMonth(String dat) {// yyyy-MM-dd
        String str = dat.substring(0, 8);
        String month = dat.substring(5, 7);
        int mon = Integer.parseInt(month);
        if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8
                || mon == 10 || mon == 12) {
            str += "31";
        } else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
            str += "30";
        } else {
            if (isLeapYear(dat)) {
                str += "29";
            } else {
                str += "28";
            }
        }
        return str;
    }

    /**
     * 判断二个时间是否在同一个周
     *
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameWeekDates(Date date1, Date date2) {
        Calendar cal1 = DateUtils.getCalendar();
        Calendar cal2 = DateUtils.getCalendar();
        cal1.setTime(date1);
        cal2.setTime(date2);
        int subYear = cal1.get(Calendar.YEAR) - cal2.get(Calendar.YEAR);
        if (0 == subYear) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
                    .get(Calendar.WEEK_OF_YEAR))
                return true;
        } else if (1 == subYear && Calendar.DECEMBER == cal2.get(Calendar.MONTH)) {
            // 如果12月的最后一周横跨来年第一周的话则最后一周即算做来年的第一周
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
                    .get(Calendar.WEEK_OF_YEAR))
                return true;
        } else if (-1 == subYear && Calendar.DECEMBER == cal1.get(Calendar.MONTH)) {
            if (cal1.get(Calendar.WEEK_OF_YEAR) == cal2
                    .get(Calendar.WEEK_OF_YEAR))
                return true;
        }
        return false;
    }

    /**
     * 产生周序列,即得到当前时间所在的年度是第几周
     *
     * @return
     */
    public static String getSeqWeek() {
        Calendar c = DateUtils.getCalendar();
        String week = Integer.toString(c.get(Calendar.WEEK_OF_YEAR));
        if (week.length() == 1)
            week = "0" + week;
        String year = Integer.toString(c.get(Calendar.YEAR));
        return year + week;
    }


    /**
     * 一天为24*60*60*1000 = 86400000毫秒
     */
    public static final long ONE_DAY_MILLIS = 86400000;
    /**
     * 一小时为60*60=3600秒
     */
    public static final int ONE_HOUR_SECONDS = 3600;
    /**
     * 一小时为60*60*1000=3600000毫秒
     */
    public static final long ONE_HOUR_MILLIS = 3600000;
    /**
     * 一分钟为60秒
     */
    public static final int ONE_MINUTES_SECONDS = 60;

    /**
     * 一分钟为60*1000=600000毫秒
     */
    public static final long ONE_MINUTES_MILILLS = 60000;

    /**
     * 后天之后
     */
    public static final int DAYS_LATER = 32;
    /**
     * 后天
     */
    public static final int DAY_AFTER_TOMMORROW = 31;
    /**
     * 明天
     */
    public static final int DAY_TOMMORROW = 30;
    /**
     * 今天
     */
    public static final int DAY_TODAY = 1;
    /**
     * 刚刚
     */
    public static final int JUST = 25;
    /**
     * 一分钟前
     */
    public static final int ONE_MINUTE_BEFORE = 26;
    /**
     * 5分钟前
     */
    public static final int FIVE_MINUTE_BEFORE = 27;
    /**
     * 10分钟前
     */
    public static final int TEN_MINUTE_BEFORE = 28;
    /**
     * 30分钟前
     */
    public static final int THIRTY_MINUTE_BEFORE = 29;
    /**
     * 未知时间
     */
    public static final int DAY_UNKNOW = 0;
    /**
     * 昨天
     */
    public static final int DAY_YESTERDAY = -1;
    /**
     * 前天
     */
    public static final int DAY_BEFORE_YESTERDAY = -2;
    /**
     * 前天之前
     */
    public static final int DAYS_FARTHER = -3;

    /**
     * 返回   xx时xx分xx秒  格式的时分秒
     * @param l
     * @return xx时xx分xx秒
     */
    public static String format2时_分_秒(Long l) {
        long hour = 0;
        long minute = 0;
        long second = l;
        if (second > 60) {
            minute = second / 60;//取整
            second = second % 60;//取余
        }
        if (minute > 60) {
            hour = minute / 60;
            minute = minute % 60;
        }
        return hour + "时" + minute + "分" + second + "秒";
    }
    // a integer to xx:xx:xx
    /**
     * 返回  xx:xx:xx格式的时分秒
     * @param time
     * @return xx:xx:xx
     */
    public static String secToTime(long time) {
        String timeStr = null;
        long hour = 0;
        long minute = 0;
        long second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 23)
                    return "23:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }

    public static String unitFormat(long i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Long.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }

	/* 判断时间 */

    /**
     * 给出一个时间的描述。比如“昨天 12:30”。
     *
     * @param time
     * @param defaultPattern 当time不在前天与后天之，则调用此默认格式初始化时间描述。 <br>
     *                       如果为null，将默认使用“yyyy-MM-dd”作为时间描述
     * @return 时间的描述
     */
    public static String getDescription(long time, String defaultPattern) {
        long currentTime = System.currentTimeMillis();

        // 获取结尾的 小时和分钟
        String endTime = getFormatedTimeStr(time, "HH:mm");
        int whatDay = whatDay(new Date(time), new Date(currentTime));

        switch (whatDay) {
//            case DAY_TODAY:
//                return "今天 " + endTime;
            case JUST:
                return "刚刚";
            case ONE_MINUTE_BEFORE:
                return "1分钟前";
            case FIVE_MINUTE_BEFORE:
                return "5分钟前";
            case TEN_MINUTE_BEFORE:
                return "10分钟前";
            case THIRTY_MINUTE_BEFORE:
                return "30分钟前";

            case DAY_YESTERDAY:
                return "昨天 " + endTime;

            case DAY_TOMMORROW:
                return "明天 " + endTime;

            case DAY_BEFORE_YESTERDAY:
                return "前天 " + endTime;

            case DAY_AFTER_TOMMORROW:
                return "后天 " + endTime;

            default:
                if (1<= whatDay && whatDay <= 23){
                    return whatDay+"小时前";
//                }else if (JUST<=whatDay && whatDay<= THIRTY_MINUTE_BEFORE){
//                    switch ()
                }else {
                    return getFormatedTimeStr(time, defaultPattern != null ? defaultPattern : "yyyy-MM-dd");
                }
        }
    }

    /**
     * 按照时间格式给出一个时间的描述。比如“2014年8月17日 09:50”
     *
     * @param time
     * @param pattern 时间的格式。如果为null，则默认使用
     *                “yyyy-MM-dd HH:mm”
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String getFormatedTimeStr(long time, String pattern) {
        try {
            Date date = new Date(time);
            SimpleDateFormat format = getSimpleDateFormat(!StringUtils.isEmpty(pattern) ? pattern : "yyyy-MM-dd   HH:mm");
            return format.format(date);
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }

    }

    /**
     * 判断judgeTime相对于relativeTime的时间，比如说今天，昨天，或者前天
     *
     * @param judgeTime    需要判断的时间，比如昨天。
     * @param relativeTime 相对的时间，比如今天。如果为null的话，将默认设置为今天
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static int whatDay(Date judgeTime, Date relativeTime) {
//        long currentTime = System.currentTimeMillis();
        if (relativeTime == null) {
            relativeTime = new Date(System.currentTimeMillis());
        }

        long relatetimeLong = relativeTime.getTime();// 被轉換成當天零點之前的時間戳
        SimpleDateFormat format = getSimpleDateFormat("yyyy-MM-dd");
        String relativeTimeStr = format.format(relativeTime);
        // 获取今天0点的时间
        Date relativeDay;
        try {
            relativeDay = format.parse(relativeTimeStr);

            // 用judgeTime减去相对时间的那天的0点。
            long millis = judgeTime.getTime() - relativeDay.getTime();

            if (millis >= 0) {
                // 今天0点之后的时间
                if (millis < ONE_DAY_MILLIS) {
                    // 是今天内的情況下，將時間將其時間戳的差值換成為轉化成零點的時間戳
                    millis = Math.abs(judgeTime.getTime() - relatetimeLong);
                    LogUtils.d(" millis ="+ millis);
                    // 1天之内
                    if (millis <= ONE_MINUTES_MILILLS) {
                        return JUST;
                    } else if (millis <= ONE_MINUTES_MILILLS * 5) {
                        return ONE_MINUTE_BEFORE;
                    } else if (millis <= ONE_MINUTES_MILILLS * 10) {
                        return FIVE_MINUTE_BEFORE;
                    } else if (millis <= ONE_MINUTES_MILILLS * 30) {
                        return TEN_MINUTE_BEFORE;
                    }else if (millis <= ONE_HOUR_MILLIS) {
                        return THIRTY_MINUTE_BEFORE;
                    }else{
                        return (int) (millis / ONE_HOUR_MILLIS);
                    }
                } else if (millis >= ONE_DAY_MILLIS && millis < 2 * ONE_DAY_MILLIS) {
                    // 1~2天之内
                    return DAY_TOMMORROW;
                } else if (millis >= 2 * ONE_DAY_MILLIS && millis < 3 * ONE_DAY_MILLIS) {
                    // 2~3之内
                    return DAY_AFTER_TOMMORROW;
                } else {
                    return DAYS_LATER;
                }
            } else {
                // 今天0点之前的时间
                millis = -millis;
                if (millis < ONE_DAY_MILLIS) {
                    // 之前1天之内
                    return DAY_YESTERDAY;
                } else if (millis >= ONE_DAY_MILLIS && millis < 2 * ONE_DAY_MILLIS) {
                    // 之前1~2天之内
                    return DAY_BEFORE_YESTERDAY;
                } else {
                    return DAYS_FARTHER;
                }
            }

        } catch (ParseException e) {
            e.printStackTrace();
            // 报错则默认是unknow
            return DAY_UNKNOW;
        }
    }

    /**
     * 添加日历事件
     *
     * @param startMillis 事件开始的时间
     * @param endMillis   事件结束的时间
     * @param mimus       提前通知的分钟数
     * @param evTitle     事件的标题
     * @param evContent   事件的具体内容
     * @param evLocation  事件的地点
     * @return 添加到日程是否成功
     */
    public static String addEventToSysterms(long startMillis, long endMillis, int mimus, String evTitle, String evContent, String evLocation) {
        String myEventsId;
        Context ctx = BaseApp.instance();
        evTitle = StringUtils.isEmpty(evTitle) ? "" : evTitle;
        evContent = StringUtils.isEmpty(evContent) ? "" : evContent;
        evLocation = StringUtils.isEmpty(evLocation) ? "" : evLocation;
        long nowTime = new Date().getTime();
        if (startMillis < nowTime || endMillis < nowTime || (nowTime + mimus * 60 * 1000) > startMillis) {
            return null;
        }

        String calanderURL = "";
        String calanderEventURL = "";
        String calanderRemiderURL = "";
        if (Integer.parseInt(Build.VERSION.SDK) >= 8) {
            calanderURL = "content://com.android.calendar/calendars";
            calanderEventURL = "content://com.android.calendar/events";
            calanderRemiderURL = "content://com.android.calendar/reminders";

        } else {
            calanderURL = "content://calendar/calendars";
            calanderEventURL = "content://calendar/events";
            calanderRemiderURL = "content://calendar/reminders";
        }
        try {

            // 获取要出入的gmail账户的id
            String calID = "";
            Cursor userCursor = BaseApp.instance().getContentResolver().query(Uri.parse(calanderURL), null,
                    null, null, null);
            if (userCursor.getCount() > 0) {
                userCursor.moveToFirst();
                calID = userCursor.getString(userCursor.getColumnIndex("_id"));
            }

            ContentValues eValues = new ContentValues();  //插入事件
            TimeZone tz = TimeZone.getDefault();//获取默认时区

            //插入日程
            eValues.put(CalendarContract.Events.DTSTART, startMillis);
            eValues.put(CalendarContract.Events.DTEND, endMillis);
            eValues.put(CalendarContract.Events.TITLE, evTitle);
            eValues.put(CalendarContract.Events.HAS_ALARM, 1);
            eValues.put(CalendarContract.Events.DESCRIPTION, evContent);
            eValues.put(CalendarContract.Events.CALENDAR_ID, calID);
            LogUtils.d("----------------calID:" + calID);
            eValues.put(CalendarContract.Events.EVENT_LOCATION, evLocation);
            eValues.put(CalendarContract.Events.EVENT_TIMEZONE, tz.getID().toString());
            Uri uri = ctx.getContentResolver().insert(Uri.parse(calanderEventURL), eValues);

            ContentValues rValues = new ContentValues();  //插入提醒，与事件配合起来才有效
            //插完日程之后必须再插入以下代码段才能实现提醒功能
            myEventsId = uri.getLastPathSegment(); // 得到当前表的_id
            rValues.put("event_id", myEventsId);
            rValues.put(CalendarContract.Reminders.EVENT_ID, myEventsId);
            rValues.put(CalendarContract.Reminders.MINUTES, mimus);    //提前10分钟提醒
            rValues.put(CalendarContract.Reminders.METHOD, CalendarContract.Reminders.METHOD_ALERT);    //如果需要有提醒,必须要有这一行
            ctx.getContentResolver().insert(Uri.parse(calanderRemiderURL), rValues);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return myEventsId;
    }

    /**
     * 删除日程
     */
    public static int deleteEventFromSystem(String id) {
        LogUtils.d("------------------remove eventId:" + id);
        if (TextUtils.isEmpty(id)) {
            return 0;
        }
        long eventid = 0;
        try {
            eventid = Long.valueOf(id);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return 0;
        }
        Uri deleteUri = ContentUris.withAppendedId(CalendarContract.Events.CONTENT_URI, eventid);
        int rows = BaseApp.instance().getContentResolver().delete(deleteUri, null, null);
        return rows;
    }

    private Locale sLocale = Locale.getDefault();
    private TimeZone sTimeZone = TimeZone.getDefault();

    public void setsLocale(Locale sLocale) {
        this.sLocale = sLocale;
    }

    public void setsTimeZone(TimeZone sTimeZone) {
        this.sTimeZone = sTimeZone;
    }

    /**
     * 获取时间Format
     * @param pattern
     * @return
     */
    public static SimpleDateFormat getSimpleDateFormat(@Nullable String pattern){
        SimpleDateFormat sdf = new SimpleDateFormat(!StringUtils.isEmpty(pattern) ? pattern : "yyyy-MM-dd HH:mm" ,Locale.CHINESE);
        sdf.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        return sdf;
    }
    /**
     * 获取日历
     * @return
     */
    public static Calendar getCalendar(){
        return Calendar.getInstance(TimeZone.getTimeZone("GMT+8"), Locale.CHINESE);
    }
}
